package com.yiwei.hdb.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.hdb.R;
import com.yiwei.hdb.interfaces.OnRecyclerViewListener;
import com.yiwei.hdb.model.MessageModel;
import com.yiwei.hdb.util.RoundedBitmapUtils;

import java.util.List;


/**
 * 消息中心
 * Created by MasterDong on 16/4/12.
 */

public class MessageAdapter extends RecyclerView.Adapter {

    private static int GRAY = 0;
    private static int GRAY_DARK = 0;

    private OnRecyclerViewListener onRecyclerViewListener;

    public void setOnRecyclerViewListener(OnRecyclerViewListener onRecyclerViewListener) {
        this.onRecyclerViewListener = onRecyclerViewListener;
    }

    private Context mContext;
    private List<MessageModel.DataList> list;

    public MessageAdapter(Context context, List<MessageModel.DataList> list) {
        this.list = list;
        this.mContext = context;
        GRAY = ContextCompat.getColor(mContext, R.color.gray);
        GRAY_DARK = ContextCompat.getColor(mContext, R.color.gray_dark);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.message_items, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        view.setLayoutParams(lp);
        return new DealViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        DealViewHolder holder = (DealViewHolder) viewHolder;
        holder.position = i;
        MessageModel.DataList model = list.get(i);
        holder.time.setText(model.getCreate_time_format());
        holder.title.setText(model.getTitle());
        holder.date.setText(model.getDate());
        holder.content.setText(model.getContent());
        RoundedBitmapUtils.setRoundedDrawable(mContext.getResources(),holder.icon,R.mipmap.message_icon);


        if (Integer.parseInt(model.getRead_time()) <= 0) {
            holder.title.setTextColor(GRAY_DARK);
            holder.date.setTextColor(GRAY_DARK);
            holder.content.setTextColor(GRAY_DARK);
            holder.title.setTextColor(GRAY_DARK);
            holder.see.setTextColor(GRAY_DARK);
        } else {
            holder.title.setTextColor(GRAY);
            holder.date.setTextColor(GRAY);
            holder.content.setTextColor(GRAY);
            holder.title.setTextColor(GRAY);
            holder.see.setTextColor(GRAY);
        }
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addList(List<MessageModel.DataList> list) {
        this.list.addAll(list);
    }

    public void clearList() {
        this.list.clear();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class DealViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public View rootView;
        public TextView title;
        public TextView content;
        public TextView date;
        public TextView time;
        public TextView see;
        public ImageView icon;
        public int position;

        public DealViewHolder(View itemView) {
            super(itemView);
            date = (TextView) itemView.findViewById(R.id.date);
            content = (TextView) itemView.findViewById(R.id.content);
            title = (TextView) itemView.findViewById(R.id.title);
            time = (TextView) itemView.findViewById(R.id.time);
            see = (TextView) itemView.findViewById(R.id.see);
            icon = (ImageView) itemView.findViewById(R.id.icon);

            rootView = itemView.findViewById(R.id.view);
            rootView.setOnClickListener(this);
            rootView.setOnLongClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (null != onRecyclerViewListener) {
                onRecyclerViewListener.onItemClick(list, position);
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (null != onRecyclerViewListener) {
                return onRecyclerViewListener.onItemLongClick(list, position);
            }
            return false;
        }

    }
}

